function postJSON(url, data, callback, error) {
   $.ajax({ 
        url: url,
        type: 'post',
        contentType: 'application/json',
        data: JSON.stringify(data),
        dataType: 'json',
        success: function(data) {
            callback(data);
        },
        error: function(req, message, e) {
            console.log(message);

            if ( error ) {
                error(req, message, e);
            }
        }
    });
}

$(document).ready(function() {
    postJSON('/caregiver/search', {
        },
        function(data) {
            if ( !data.successful ) {
                console.log(data);
                return;
            }

            data.data.caregivers.forEach(function(caregiver) {
                var html = 
                    '<div class="care-giver">' + 
                        '<div class="giver-photo">' +
                            '<img src="/upload/' + caregiver.photo_url + '">' +
                        '</div>' +
                        '<div class="giver-simple-info">' +
                            '<span class="giver-name-age">' + caregiver.user.name + 
                                '<span>'+ caregiver.user.age + '</span>岁</span>' +
                            '<div class="working-experience">' +
                                '工作经验：<span>' + caregiver.working_years + '年</span>' +
                            '</div>' +
                        '</div>' +
                    '</div>';
                $('#care-giver-box > div').append(html);
            });
        }
    );
    $('#btn-search-caregiver').click(function(e) {
        e.preventDefault();

        var age = $('#select-age').val();
        var workingYears = $('#select-working-years').val();
        var educationBackground = $('#select-edu-bkgd').val();

        var url = '/care_font/caregiver/caregiver.html?' +
            'age=' + age + '&' +
            'working_years=' + workingYears + '&' +
            'edu_bkgd=' + educationBackground;

        window.open(url);
    });
    //$('#btn-find-user').click(function(e) {
    //    e.preventDefault();

    //    var userId = $('#user-id').val();
    //    var user = {
    //        id: userId
    //    }

    //    postJSON('/user/find', {
    //            user: user
    //        },
    //        function(data) {
    //            $('#message-box').html(JSON.stringify(data));
    //        }
    //    );
    //});
});
